#include "TutorApp5.h"

#ifdef WIN32
#ifdef Tutorial5_EXPORTS
#define TUTORAPP5_API __declspec(dllexport)
#else
#define TUTORAPP5_API __declspec(dllimport)
#endif
#else
#define TUTORAPP5_API
#endif

extern "C" TUTORAPP5_API Blobz::BaseApplication* GetInstance()
{
	Tutorial5::TutorApp5* pTutorial = new Tutorial5::TutorApp5();
	return (Blobz::BaseApplication*)pTutorial;
}

extern "C" TUTORAPP5_API unsigned int GetMajorReleaseVersion()
{
	return NUI_RELEASE_MAJOR_VER;
}

extern "C" TUTORAPP5_API unsigned int GetMinorReleaseVersion()
{
	return NUI_RELEASE_MINOR_VER;
}

extern "C" TUTORAPP5_API const char* GetAppName()
{
	return "Tutorial5";
}

namespace Tutorial5
{
	
	/**
	* Constructor
	*/
	TutorApp5::TutorApp5()
	{
	}
	
	/**
	* Destructor
	*/
	TutorApp5::~TutorApp5()
	{
	}

	/**
	* Loads and setups up all the required objects and gui elements
	*/
	bool TutorApp5::onInitialize()
	{
		// Tell the application to use a fixed aspect ratio so that our view content is not stretched
		getWindow().setUseFixedRatio(true);

		// Save the application directory for easier access
		QString appDir = getApplicationDirectory();
		
		// save the application window aspect ratio
		float aspectRatio = getWindow().getWindowAspect();

		// load the background texture
		mBackgroundTexture.load(getApplicationDirectory()+"bg.png", true);

		//load the video
		mVideo.load(appDir, appDir+"nuiteq.mp4");

		// set position, scale and rotation of the video
		mVideo.setPosition(QVector2D(aspectRatio/2.f, 0.5f));
		mVideo.setScale(0.4f);
		mVideo.setScaleLimits(0.04f, 4.0f);
		mVideo.setRotation(Blobz::degToRad(20.f));

		return true;
	}
	
	/**
	* On Reload Handler
	*/
	void TutorApp5::onReload()
	{
	}
	
	/**
	* Unload all loaded objects
	*/
	void TutorApp5::onDeinitialize()
	{
		// unload all objects
		mBackgroundTexture.unload();
		mVideo.unload();
	}
	
	/**
	* Update event handler
	*/
	void TutorApp5::onUpdate(float inFrameTime)
	{
		// send update events to all objects
		mVideo.onUpdate(inFrameTime);

		if(mVideo.getPosition().x() < 0)
			mVideo.setPosX(0);
		else if(mVideo.getPosition().x() > getWindow().getWindowAspect())
			mVideo.setPosX(getWindow().getWindowAspect());
		if(mVideo.getPosition().y() < 0)
			mVideo.setPosY(0);
		else if(mVideo.getPosition().y() > 1)
			mVideo.setPosY(1);
	}
	
	/**
	* Render event handler
	*/
	void TutorApp5::onDraw()
	{
		// tell Snowflake that we want to start rendering
		getWindow().enableRendering();

		// screen setup, part of the Snowflake framework
		// clears the background to white
		getWindow().defaultRenderSetup(QVector4D(1,1,1,1));

		// Render the background texture
		const QVector2D windowSize(getWindow().getWindowAspect(), 1.f);		
		const QVector2D mPosition(0.f, 0.f);

		// Enables alpha blending
		glEnable(GL_BLEND);
		glDisable(GL_DEPTH_TEST);
		glEnable(GL_TEXTURE_2D);

		// Binds the texture ID to OpenGL so it can be used
		glBindTexture(GL_TEXTURE_2D, mBackgroundTexture.getID()); 
		// Begin drawing GLQuads
		glBegin(GL_QUADS);
		glColor4f(1, 1, 1, 1);
		glTexCoord2f(0,1);
		glVertex2f(mPosition.x(), mPosition.y());
		glTexCoord2f(1,1);
		glVertex2f(mPosition.x()+windowSize.x(), mPosition.y());
		glTexCoord2f(1,0);
		glVertex2f(mPosition.x()+windowSize.x(), mPosition.y()+windowSize.y());
		glTexCoord2f(0,0);
		glVertex2f(mPosition.x(), mPosition.y()+windowSize.y());
		glEnd();
		glDisable(GL_TEXTURE_2D);

		glColor4f(1,1,1,1);

		//render the video
		mVideo.onRender();

		// tell Snowflake that we're done rendering
		getWindow().disableRendering();
	}

	/**
	* Triggered when there is a finger down event on the screen
	* \param inData holds the touch information
	*/
	void TutorApp5::onFingerDown(const TouchListener::TouchData& inData)
	{
		mVideo.onFingerDown(inData);
	}

	/**
	* Triggered when there is a finger up event on the screen
	* \param inData holds the touch information
	*/
	void TutorApp5::onFingerUp(const TouchListener::TouchData& inData)
	{
		mVideo.onFingerUp(inData);
	}

	/**
	* Triggered when there is a finger update event on the screen
	* \param inData holds the touch information
	*/
	void TutorApp5::onFingerUpdate(const TouchListener::TouchData& inData)
	{
		mVideo.onFingerUpdate(inData);
	}

	/**
	* Triggered when there is a finger moves out of the screen or application window
	* \param inData holds the touch information
	*/
	void TutorApp5::onFingerExit(const TouchListener::TouchData& inData)
	{
		// just treat finger exit events as finger up events
		onFingerUp(inData);
	}
}
